

(function(){

/**********************************
 * class Contents
 * 命令: #<sysId>/<clsId>/add|modify|remove|listJson|detailJson
 */



/**
 * 构造函数
 */
var Contents = window.Contents = function( procFile,sysId,clsId,catClsId )
{
    // 驱动属性
    this.procFile = procFile;
    this.sysId = sysId;
    this.clsId = clsId;
    this.catClsId = catClsId;

    this.topPage = $("#Column-B");

    this.sysClsCmd = this.sysId+"/"+this.clsId
    this.sysClsId = this.sysId + "_" + this.clsId;
}


//Contents.fn = Contents.prototype = new Contents( this.procFile, this.sysId, this.clsId, this.catClsId );


Contents.fn = Contents.prototype =
{
    /**
     * 监听
     */
    listening : function()
    {
        if( false == Command.allowAccess( "#"+this.sysClsCmd, location.hash ) ){ return false;  }

        this.list();
        this.detail();
        this.add();
        this.modify();
        this.remove();
    },

    /**
     * 文章列表页面,news/"+this.clsId+"/list
     */
    list : function()
    {
        // 判断命令
        var hash = location.hash;
        if( false == Command.allowAccess( "#"+this.sysClsCmd+"/list", hash ) ){ return false;  }

        // 读取模板
        var page = $("#"+this.sysClsId+"_list").clone();
        page[0].id =     this.sysClsId+"_list_clone";

        // 当前位置
        var a  = hash.split( '/' ),
        fCatId = Command.back( hash,0 );
        bCatId = Command.back( hash,1 );
        page.find(".btnReturn").attr( "href", "#"+this.sysId+"/"+this.catClsId+"/list" + bCatId );
        page.find(".btnAdd").attr( "href","#"+this.sysClsCmd+"/add" + fCatId );

        // 读取数据
        var _ = this;
        $.getJSON( _.procFile+"?cmd="+_.sysClsCmd+"/listJson&catId=" + a[a.length-1], function( json ){
            var s = "<ul>";
            if( 'undefined' != typeof json.list.id ){

                for( var i=0; i<json.list.id.length; i++ ){

                    s += '<li><span>' + i + '</span>';
                    s += '<h1><a href="#'+_.sysClsCmd+'/detail' + fCatId + '/' + json.list.id[i] + '">' + $.htmlEncode(json.list.title[i]) + '</a></h1>';
                    s += '<span>' + $.htmlEncode(json.list.author[i]) + '</span>';
                    s += '<a href="#'+_.sysClsCmd+'/modify' + fCatId + '/' + json.list.id[i] + '">修改</a> ';
                    s += '<a href="#'+_.sysClsCmd+'/remove' + fCatId + '/' + json.list.id[i] + '">删除</a></li>';
                }
            }else{
                s += '<li>该分类下没有文章!</li>';
            }

            s += '</ul>';
            page.append( $(s) );

        });
        this.topPage.empty().append( page );
    },


    /**
     * 文章详细页面,news/"+this.clsId+"/detail
     */
    detail : function()
    {
        // 判断命令
        var hash = location.hash;
        if( false == Command.allowAccess( "#"+this.sysClsCmd+"/detail", hash ) ){ return false;  }

        // 读取模板
        var page = $("#"+this.sysClsId+"_detail").clone();
        page[0].id =     this.sysClsId+"_detail_clone";

        // 当前位置
        var a = hash.split( '/' ),
            bCatId = Command.back( hash, 1 );
        page.find(".btnReturn").attr("href", "#"+this.sysClsCmd+"/list" + bCatId );

        // 读取数据
        var _ = this;
        $.getJSON( _.procFile+"?cmd="+_.sysClsCmd+"/detailJson&id=" + a[a.length-1], function( json ){

            var s = "<div>";
            s += "<h1>" + $.htmlEncode(json.title) + "</h1>";
            s += "<p><pre>" + $.htmlEncode(json.content) + "</pre></p>";
            s += "</div>";

            page.append( $(s) );
            _.topPage.empty().append( page );
        });
    },


    /**
     * 添加文章界面,news_"+this.clsId+"_add
     */
    add : function()
    {
        // 判断命令
        var hash = location.hash;
        if( false == Command.allowAccess( "#"+this.sysClsCmd+"/add", hash ) ){ return false;  }

        // 读取模板
        var page = $("#"+this.sysClsId+"_add").clone();
        page[0].id =     this.sysClsId+"_add_clone";

        // 当前位置
        var a = hash.split( '/' );
        var catId =  0,
            bCatId= Command.back( hash, 0 );
        if( 3 < a.length  ){ catId = a[a.length-1];   }

        // 分类选择框
        if( 0 < catId )
        {
            Categories.SelectOption( this.procFile, page.find('.selectOptions'), this.sysId, this.catClsId, catId );
            page.find(".btnReturn").attr( "href","#"+this.sysClsCmd+"/list" + bCatId );
        }
        else
        {
            Categories.SelectOptions( this.procFile, page.find('.selectOptions'), this.sysId, this.catClsId, 0, catId );
            page.find(".btnReturn").attr( "href","#"+this.sysId+"/"+this.catClsId+"/list" );
        }

        // 添加到可视区域
        this.topPage.empty().append( page );

        // 监听提交
        this.submit_add( page.find('.frm') );
    },
    submit_add : function( frm )
    {
        frm.ajaxSubmit();
    },


    /**
     * 修改文章页面,news/"+this.clsId+"/modify
     */
    modify : function()
    {
        // 判断命令
        var hash = location.hash;
        if( false == Command.allowAccess( "#"+this.sysClsCmd+"/modify", hash ) ){ return false;  }

        // 读取模板
        var page = $("#"+this.sysClsId+"_modify").clone();
        page[0].id =     this.sysClsId+"_modifyclone";

        // 当前位置
        var a = hash.split( '/' ),
            bCatId = Command.back( hash, 1 );
        page.find(".btnReturn").attr("href", "#"+this.sysClsCmd+"/list" + bCatId );

        // 读取数据
        var _ = this;
        $.getJSON( _.procFile+"?cmd="+_.sysClsCmd+"/detailJson&id="+ a[a.length-1], function( json ){

            page.find("[name='id']").val( json.id );
            page.find("[name='title']").val( json.title );
            page.find("[name='content']").val( $.htmlEncode(json.content) );
            page.find("[name='author']").val( json.author );

        });

        this.topPage.empty().append( page );
        this.submit_modify( page.find(".frm") );
    },
    submit_modify : function( frm )
    {
        frm.ajaxSubmit();
    },


    /**
     * 页面, news/"+this.clsId+"/remove
     */
    remove : function()
    {
        // 判断命令
        var hash = location.hash;
        if( false == Command.allowAccess( "#"+this.sysClsCmd+"/remove", hash ) ){ return false;  }

        // 当前位置
        var a = hash.split( '/' ),
            bCatId = Command.back( hash, 1 );

        // 弹出提示
        if( !confirm("确定要删除？") ){ return false;  }

        // 读取数据
        var _ = this;
        $.get(_.procFile+"?cmd="+_.sysClsCmd+"/remove&id=" + a[a.length-1],function( dat ){
            alert( dat );   // 弹出提示
        });

        location.hash = "#"+_.sysClsCmd+"/list" + bCatId;
        this.list();
    }
}//class Contents.fn

})();
